/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
layui.define(['form','table',"element",'pkc','laypage','format'],function (e) {
    var t = layui.$;
    var layform = layui.form;
    var laytable = layui.table;
    var setter = layui.setter;
    var r = (layui.form,layui.element);
    var laypage = layui.laypage;
    var format = layui.format;
    var pkc = layui.pkc;

    var form = {
        formData: null,
        //加载表格
        initGrid: function(){
            laytable.init('LAY-user-back-manage',{
                elem: "#LAY-user-back-manage",
                id: "LAY-user-back-manage",
                cellMinWidth: 80,
                cols: [[{
                    field: "ParkId",
                    width: "15%",
                    title: "相关园区",
                    align: "center"
                },{
                    field: "SendOrderId",
                    width: "15%",
                    title: "关联的派单",
                    align: "center"
                },{
                    field: "OperatorId",
                    width: "10%",
                    title: "调度人员",
                    align: "center"
                },{
                    field: "FSpaceId",
                    width: "10%",
                    title: "原车位",
                    align: "center"
                },{
                    field: "SpaceId",
                    width: "10%",
                    title: "现车位",
                    align: "center"
                },{
                    field: "CarId",
                    width: "10%",
                    title: "相关车辆",
                    align: "center"
                },{
                    field: "Remark",
                    width: "15%",
                    title: "备注",
                    align: "center",
                    templet: "#StatusTpl"
                },{
                    title:"操作",
                    width: "20%",
                    align:"center",
                    fixed: "right",
                    templet: "#table-system-order"
                }]],
                toolbar: "#toolbar",
                defaultToolbar:[''],
                text: {
                    none: "暂无相关数据"
                },
                done: (res, curr, count) => {
                    t(window).resize();
                    r.render("progress");
                    count || t('div.layui-table-box>div').css('overflow', 'auto');
                }
            })
        },
        /**
         * 初始化分页
         * @param {number} count 总数
         * @param {number} pindex 当前页
         */
        initPage: function (count, pindex) {
            laypage.render({
            elem: "apppage",
            count: count,
            layout: ['limit', 'prev', 'page', 'next', 'count', 'refresh', 'skip'],
            jump: function (obj, first) {
                //obj包含了当前分页的所有参数，比如：
                // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                // console.log(obj.limit); //得到每页显示的条数
    
                //首次不执行
                if (!first) {
                //do something
                form.loadData(obj.curr, obj.limit);
                }
            },
            curr: pindex
            });
        },
        //加载 仓库 下拉菜单
        loadWarehouse: function (id) {
            if(!id){
            return;
            }
            var data = {};
            var aDataStr = "";
            var qData = "";
            data["Token"] = layui.data(setter.tableName)["data"][0]["value"];
            data["CurrentPage"] = 1;
            data["PageCount"] = 0;
            data["ParkId"] = id;
            data["Nonstr"] = format.fm.randomWord(true, 10, 20);
            data["Signstr"] = md5("Token=" + data["Token"] + "Nonstr=" + data["Nonstr"] + "PageCount=" + data["PageCount"] + "CurrentPage=" + data["CurrentPage"]);
            aDataStr = format.fm.oDataTran(qData); //转化成odata格式
            return pkc.http.SendGetOdata(aDataStr, data, 'Warehouse', function (ret) {
                if (ret) {
                    // ret = JSON.parse(ret);
                    if (ret !== null && ret.Code === 1) {
                        //赋值表单
                        t.each(ret.Body.list, function (i, v) {
                            t("#selWarehouse").append(
                                "<option value='" +
                                v.Id +
                                "' " +
                                (i === '' ? "selected" : "") +
                                ">" +
                                v.No +
                                "</option>"
                            );
                            layform.render("select"); //刷新select选择框渲染
                        });
                    } else {
                        layer.alert(ret.Message);
                    }
                }
            },
                function (res) {
                    layer.alert("网络异常.请稍后再试！");
                    // layer.closeAll("loading");
                }
            );
        },
        // 加载车位
        loadSpace: function(pid, wid){
            layer.load("1");
            var qData = ""; //查询条件
            var data = {};
            var aDataStr = "";
            data["Token"] = layui.data(setter.tableName)["data"][0]["value"];
            data["CurrentPage"] = 1;
            data["PageCount"] = 0;
            data["ParkId"] = pid; 
            data["WarehouseId"] = wid;        
            data["Nonstr"] = format.fm.randomWord(true, 10, 20);
            data["Signstr"] = md5("Token=" + data["Token"] + "ParkId=" + data["ParkId"] + "Nonstr=" + data["Nonstr"]  + "PageCount=" + data["PageCount"] + "CurrentPage=" + data["CurrentPage"]);
            aDataStr = format.fm.oDataTran(qData); //转化成odata格式
            return pkc.http.SendGetOdata(aDataStr, data,'Space', function(ret){
                if(ret !== null && ret.Code === 1){
                    //赋值表单
                    var listData = [];
                        listData.push({"Id": "","No": "请选择车位"});
                        for(let i = 0;i < ret.Body.list.length; i++){
                            listData.push(ret.Body.list[i]);
                        }
                    t.each(listData, function (i, v) {
                        t("#selSpace").append(
                            "<option value='" +
                            v.Id +
                            "' " +
                            (i === '' ? "selected" : "") +
                            ">" +
                            v.No +
                            "</option>"
                        );
                        layform.render("select"); //刷新select选择框渲染
                    });
                }else{
                    layer.alert(ret.Message);
                }
            }).always(function () {
                setTimeout(() => {
                    layer.closeAll("loading");
                }, 1000);
            });
        },
        //加载 司机 下拉菜单
        loadDrivers: function (id) {
            if(!id){        
            return;
            }
            var data = {};
            var aDataStr = "";
            var qData = "";
            data["Token"] = layui.data(setter.tableName)["data"][0]["value"];
            data["CurrentPage"] = 1;
            data["PageCount"] = 0;
            data["CompanyId"] = id;
            data["Nonstr"] = format.fm.randomWord(true, 10, 20);
            data["Signstr"] = md5("Token=" + data["Token"] + "Nonstr=" + data["Nonstr"] + "PageCount=" + data["PageCount"] + "CurrentPage=" + data["CurrentPage"]);
            aDataStr = format.fm.oDataTran(qData); //转化成odata格式
            return pkc.http.SendGetOdata(aDataStr, data, 'Driver', function (ret) {
                if (ret) {
                    // ret = JSON.parse(ret);
                    if (ret !== null && ret.Code === 1) {
                        //赋值表单
                        var listData = [];
                        listData.push({"Id": "","Name": "请选择司机"});
                        for(let i = 0;i < ret.Body.list.length; i++){
                            listData.push(ret.Body.list[i]);
                        }
                        t.each(listData, function (i, v) {
                            t("#selDriver").append(
                                "<option value='" +
                                v.Id +
                                "' " +
                                (i === '' ? "selected" : "") +
                                ">" +
                                v.Name +
                                "</option>"
                            );
                            layform.render("select"); //刷新select选择框渲染
                        });
                    } else {
                        layer.alert(ret.Message);
                    }
                }
            },
                function (res) {
                    layer.alert("网络异常.请稍后再试！");
                    // layer.closeAll("loading");
                }
            );
        },
        //表格数据加载
        loadData: function(cp, pc){
            layer.load("1");
            var data = {};
            var adata = {};//需要拼接$filter的查询条件
            var aDataStr = "";
            data["Token"] = layui.data(setter.tableName)["data"][0]["value"];
            data["ParkId"] = layui.data(setter.tableName)["data"][3]["value"];
            data["CurrentPage"] = cp;
            data["PageCount"] = pc;
            data["Nonstr"] = format.fm.randomWord(true, 10, 20);
            data["Signstr"] = md5("Token=" + data["Token"] + "Nonstr=" + data["Nonstr"]  + "PageCount=" + data["PageCount"] + "CurrentPage=" + data["CurrentPage"]);
            aDataStr = format.fm.oDataTran(adata); //转化成odata格式
            return pkc.http.SendGetOdata(aDataStr, data,'Parts', function(ret){
                if(ret !== null && ret.Code === 1){
                    // 加载表格
                    laytable.reload("LAY-user-back-manage", {
                        data: ret.Body.list
                    });
                    laytable.resize("LAY-user-back-manage");
                }else{
                    layer.alert(ret.Message);
                }
            }).always(function () {
                setTimeout(() => {
                    layer.closeAll("loading");
                }, 1000);
            });
        },
        // 事件监听
        initEvt: function(){
            //新增
            laytable.on("toolbar(LAY-user-back-manage)", function(e){
                window.datas = parent.datas;
                if(e.event === "add"){
                    layer.open({
                        id: "add",
                        type: 2,
                        title: "新增调度信息",
                        content: "schedulingAdd.html",
                        area: ["600px", "500px"],
                        success: function (layero, i) {
                            window.reloadGrid = form.loadData;
                        }
                    });
                }
            });
            //编辑 & 删除
            laytable.on("tool(LAY-user-back-manage)", function(e){
                window.datas = e.data;  
                var id = e.data.Id;
                if(e.event === "detail"){
                    layer.open({
                        id: "detail",
                        type: 2,
                        title: "调度详情",
                        content: "schedulingDetail.html",
                        area: ["600px", "500px"],
                        success: function (layero, i) {
                            window.reloadGrid = form.loadData;
                        }
                    });
                };
            });
        },
        //页面初始化
        init: function(){
            form.initGrid();
            form.initPage();
            form.initEvt();
            form.loadData(1, 0);
        },
        toolAction: {
        },
        //关闭窗体
        close: function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    }

    e("partsinfo", {
        init: () => {
            form.init();
        }
    });
});